//************************************************************
//
// DATASTEP 5. GATHERING FINAL SAMPLE
//
// DESCRIPTION: DISTANCE FILE MERGE WITH FINANCIAL DATA
//
// AUTHOR: LIONEL NESTA
//
// DATASOURCE: COMPUSTAT TRANSFORMED FILES
//
//*************************************************************


// PREAMBLE

*set processors 24

clear
clear matrix 
clear mata
set more off
set mem 600m
set matsize 800
pwd
cd ..\DATA



// BUILDING TRINGULAR MATRIX

use sigma_2_digit.dta, clear
	rename gvkey1 temp
	rename gvkey2 gvkey1
	rename temp gvkey2
	order gvkey1 gvkey2
	save sigma_2_digit_2.dta, replace
	use sigma_2_digit.dta, clear
	append using sigma_2_digit_2.dta
	erase sigma_2_digit_2.dta
	replace sigma = . if abs(sigma) >= 1
save sigma_2_digit_whole.dta, replace

use sigma_2_digit_gr_corr.dta, clear
	rename gvkey1 temp
	rename gvkey2 gvkey1
	rename temp gvkey2
	order gvkey1 gvkey2
	save sigma_2_digit_gr_corr_2.dta, replace
	use sigma_2_digit_gr_corr.dta, clear
	append using sigma_2_digit_gr_corr_2.dta
	erase sigma_2_digit_gr_corr_2.dta
	replace sigma = . if abs(sigma) >= 1
save sigma_2_digit_gr_corr_whole.dta, replace



// OPENING FILE

use beta_nozeros.dta, clear
	rename gvkey1 temp
	rename gvkey2 gvkey1
	rename temp gvkey2
	order gvkey1 gvkey2
	save beta_nozeros_2.dta, replace
	use beta_nozeros.dta, clear
	append using beta_nozeros_2.dta
	replace beta = . if abs(beta)>= 1
	erase beta_nozeros_2.dta*/
	
*use cosine_techno_rta.dta, clear

mmerge gvkey1 gvkey2 year using sigma_2_digit_whole.dta, type(1:1)
*keep if _merge == 3
drop _merge
rename sigma sigma_2

mmerge gvkey1 gvkey2 year using sigma_2_digit_gr_corr_whole.dta, type(1:1)
*keep if _merge == 3
drop _merge
rename sigma sigma_2_gr_corr
replace sigma_2_gr_corr = sigma_2_gr_corr * -1
 


// MERGING FOR FIRM1

rename gvkey1 gvkey
mmerge gvkey year using fin_var.dta, type(n:1)
drop _merge

mmerge gvkey year using firm_patprod.dta, type(n:1) unmatched(master)
drop _merge

local listvar "gvkey coname iname dnum RD rd lnrd lnrdi psm_lnrd psm_lnrdi msh market_conc lny lnk lnl lnpi lnliq lnm sector patent patsmooth lncur_liab LI"
foreach x of local listvar {
	rename `x' `x'1
}

drop  Y PPE_GROSS PPE_NET TOT_ASSETS L M PI def y ppe_gross  ppe_net tot_assets m pi 

// MERGING FOR FIRM2

rename gvkey2 gvkey
mmerge gvkey year using fin_var.dta, type(n:1)
drop _merge

mmerge gvkey year using firm_patprod.dta, type(n:1) unmatched(master)
drop _merge

local listvar "gvkey coname iname dnum RD rd lnrd lnrdi psm_lnrd psm_lnrdi msh market_conc lny lnk lnl lnpi lnliq lnm sector patent patsmooth lncur_liab LI"
foreach x of local listvar {
	rename `x' `x'2
}

drop  Y PPE_GROSS PPE_NET TOT_ASSETS L M PI  def y ppe_gross  ppe_net tot_assets m pi 

// DECLARING PANEL

egen cid = group(gvkey1 gvkey2)
keep if RD1!=.
keep if RD2!=.
tsset cid year


* ADDIIOTNAL VARIABLES

// Profitability (PROXY FOR CREDIT CONSTRAINTS)

gen lnpi_k1  = lnpi1 - lnk1

gen lnpi_y1  = lnpi1 - lny1

// LIQUIDITY RATIO (CREDIT CONSTRAINTS) 1

gen lnliq_k1  = lnliq1 - lnk1

gen lnliq_y1  = lnliq1 - lny1

// LIQUIDITY RATIO (CREDIT CONSTRAINTS) 2

gen lnliq_k2  = lnliq2 - lnk2

gen lnliq_y2  = lnliq2 - lny2

// TRUE AND USED LIQUIDITY RATIO (CREDIT CONSTRAINTS) 3

gen lnliq_ratio1 = lnliq1 -  lncur_liab1

gen lnliq_ratio2 = lnliq2 -  lncur_liab2

//R&D EFFICIENCY (GAMMA PARAMETER) 1

gen lnrdpat1  =  ln(patent1) - lnrd1

gen lnrdpatsm1  =  ln(patsmooth1) - lnrd1 

//R&D EFFICIENCY (GAMMA PARAMETER) 2

gen lnrdpat2  = lnrd2 - ln(patent2)

gen lnrdpatsm2  = lnrd2 - ln(patsmooth2)

// CLEANING

drop if cid == .

// SQUARING

gen msh12 = msh1 * msh1

gen msh22 = msh2 * msh2

gen LI12 = LI1 * LI1

gen LI22 = LI2 * LI2

// AND SAVING

save final_reaction_functions_inter.dta, replace

